package com.qdu.study_app.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.qdu.study_app.R;
import com.qdu.study_app.bean.User;
import com.qdu.study_app.utils.MyToast;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class SignupActivity extends AppCompatActivity {
    private EditText phoneNum;
    private EditText password1;
    private EditText password2;
    private Button signup;
    private String phone;
    private String password;
    private String passwordRe;
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        initView();
    }
    private void initView(){
        phoneNum = (EditText) findViewById(R.id.editTextPhone);
        password1 = (EditText)findViewById(R.id.editTextTextPassword);
        password2 = (EditText)findViewById(R.id.editTextTextPassword2);
        signUp();
    }
    /*
    * 1. 注册按钮的监听事件
    * 2. 在数据库中插入一条数据
    * */
    private void signUp(){
        signup = (Button)findViewById(R.id.logup);
        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = phoneNum.getText().toString().trim();
                String password = password1.getText().toString().trim();
                String passwordRe = password2.getText().toString().trim();
                //

                if(password.equals(passwordRe)){
                    //System.out.println(phone+"-"+password+"--"+passwordRe);
                    BmobUser bmobUser = new BmobUser();
                    bmobUser.setUsername(phone);
                    bmobUser.setPassword(password);
                    bmobUser.signUp(new SaveListener<BmobUser>() {
                        @Override
                        public void done(BmobUser bmobUser, BmobException e) {
                            if(e == null){
                                MyToast.showToast(SignupActivity.this,"注册成功",Toast.LENGTH_SHORT);
                                jumpToLogin();
                            }else{
                                MyToast.showToast(SignupActivity.this,"注册失败请重试",Toast.LENGTH_SHORT);
                                toast.show();
                            }
                        }
                    });
                }

            }
        });
    }
    private void jumpToLogin(){
        Thread thread = new Thread(()->{
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           // startActivity(new Intent(SignupActivity.this,Login_first.class));
            finish();
        });
        thread.start();
    }
}